<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="nas别名" prop="nasname">
        <el-input v-model="queryParams.nasname" placeholder="请输入nas别名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="nasIp" prop="nasIp">
        <el-input v-model="queryParams.nasIp" placeholder="请输入nasip" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="默认目录" prop="path">
        <el-input v-model="queryParams.path" placeholder="请输入默认目录" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="nasconfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="nas别名" align="center" prop="nasname" />
      <el-table-column label="nasip" align="center" prop="nasIp" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="默认目录" align="center" prop="path" />
      <el-table-column label="最后快照时间" align="center" prop="lastScanTime" />
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button v-if="scope.row.lastScanTime" size="mini" type="text" icon="el-icon-thumb" @click="joinnas(scope.row)"
            v-hasPermi="['system:nasconfig:edit']">join</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog ref="dialogRef" :visible.sync="opentongji" fullscreen append-to-body destroy-on-close>
      <smb-files :dirOptions="dirOptions" :nasconfig="nasConfig" v-if="opentongji" />
    </el-dialog>


  </div>
</template>

<script>
  import {
    listNasconfig,
    getNasconfig,
    delNasconfig,
    addNasconfig,
    updateNasconfig,
    testConnect,
    getCache,
    runScan
  } from "@/api/system/nasconfig";
  import Nasinfo from '@/views/dashboard/Nasinfo'
  import SmbFiles from '@/components/SmbFiles.vue';
  export default {
    name: "Nasconfig",
    dicts: ['connect_type'],
    components: {
      SmbFiles,
      Nasinfo
    },
    data() {

      return {
        saveLoading: false,
        nasConfig: null,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // nas配置文件表格数据
        nasconfigList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        opentongji: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          nasIp: null,
          nasname: null,
          username: null,
          password: null,
          port: null,
          path: null,
          connectType: null,
        },

        dirOptions: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          nasIp: [{
            required: true,
            message: "nasip不能为空",
            trigger: "blur"
          }],
          username: [{
            required: true,
            message: "用户名不能为空",
            trigger: "blur"
          }],
          password: [{
            required: true,
            message: "密码不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {


      joinnas(row) {

      if (!row.lastScanTime) {
        this.$modal.msgError("没有快照，不能连接查询");

        return;
      }
       this.$emit('handleJoinNas', row);

      },

      scan(row) {

        this.$confirm('此操作将异步开启快照, 是否继续?', '提示', {
          confirmButtonText: '执行快照',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true;
          runScan(row).then(response => {
            this.$modal.msgSuccess("异步快照已经开启，当列表中最后快照时间更新后表示本次快照完成！请耐心等待");
            this.loading = false;
          }).catch(e => { // 使用 catch 来处理错误
            console.error(e); // 可以在这里添加更多的错误处理逻辑
            this.$modal.msgError("执行异步快照时发生错误，请检查NAS配置是否正确或稍后再试。");
            this.loading = false;
          });

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
          this.loading = false;
        });


      },
      /** 查询nas配置文件列表 */
      getList() {
        this.loading = true;
        listNasconfig(this.queryParams).then(response => {
          this.nasconfigList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          nasId: null,
          nasIp: null,
          nasname: null,
          username: null,
          password: null,
          port: 445,
          path: null,
          connectType: "smb",
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.nasId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加nas配置文件";
      },

      handleCopy(row) {
        row.nasId = null;
        this.form = row;

        this.form.password = '';
        this.open = true;
        this.title = "复制NAS配置文件";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const nasId = row.nasId || this.ids
        getNasconfig(nasId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改nas配置文件";
        });
      },
      /**
       * 测试连接nas
       */
      testConnect() {


        this.$refs["form"].validate(valid => {
          if (valid) {
            testConnect(this.form).then(response => {
              this.$modal.msgSuccess("连接成功");
              // this.open = false;
              //   this.getList();
            });
          }
        });

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {

            this.saveLoading = true;
            if (this.form.nasId != null) {
              updateNasconfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.saveLoading = false;
              });
            } else {
              addNasconfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.saveLoading = true;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const nasIds = row.nasId || this.ids;
        this.$modal.confirm('是否确认删除nas配置文件编号为"' + nasIds + '"的数据项？').then(function() {
          return delNasconfig(nasIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/nasconfig/export', {
          ...this.queryParams
        }, `nasconfig_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style lang="scss" scoped>
  .panel-group {
    margin-top: 18px;

    .card-panel-col {
      margin-bottom: 32px;
    }

    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #40c9c6;
        }

        .icon-message {
          background: #36a3f7;
        }

        .icon-money {
          background: #f4516c;
        }

        .icon-shopping {
          background: #34bfa3
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-message {
        color: #36a3f7;
      }

      .icon-money {
        color: #f4516c;
      }

      .icon-shopping {
        color: #34bfa3
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width:550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>
